package com.emo.wowoblog.service.impl;


import com.emo.wowoblog.dao.ArticleDao;
import com.emo.wowoblog.service.ArticleService;
import com.emo.wowoblog.utils.Pager;
import com.emo.wowoblog.view.ArticleView;
import com.emo.wowoblog.entity.Article;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleServiceImpl  implements ArticleService {


    @Resource
    private ArticleDao articleDao;
    @Override
    public ArticleView queryArticleViewByAid(Integer aid) {
        Object[] objectArr = articleDao.queryArticleViewByAid(aid);
        ArticleView av = null;
        if(objectArr!=null){
            av = new ArticleView();
            Object[] rowArray = (Object[])objectArr[0];
            av.setAid(Integer.parseInt(rowArray[0].toString()));
            if(rowArray[1]!=null) {
                av.setCreateTime(rowArray[1].toString());
            }
            av.setBrowseNumber(Integer.parseInt(rowArray[2].toString()));
            av.setCid(rowArray[3].toString());
            av.setCatalogName(rowArray[4].toString());
            av.setContent(rowArray[5].toString());
            av.setReplyNumber(Integer.parseInt(rowArray[6].toString()));
            av.setTags(rowArray[7].toString());
            av.setTitle(rowArray[8].toString());
            av.setQid(rowArray[9].toString());
            av.setQname(rowArray[10].toString());
            av.setUid(Integer.parseInt(rowArray[11].toString()));
            av.setUsername(rowArray[12].toString());
            return av;
        }
        return null;
    }

    @Override
    public void save(Article article) {
        articleDao.save(article);
    }

    @Override
    public List<ArticleView> queryAllArticles() {
        Object[] objectArr = articleDao.queryAllArticleViews();
        List<ArticleView> articleViewList = new ArrayList<ArticleView>();
        for(Object row: objectArr){
            ArticleView av = new ArticleView();
            Object[] rowArray = (Object[])row;
            av.setAid(Integer.parseInt(rowArray[0].toString()));
            if(rowArray[1]!=null) {
                av.setCreateTime(rowArray[1].toString());
            }
            av.setBrowseNumber(Integer.parseInt(rowArray[2].toString()));
            av.setCid(rowArray[3].toString());
            av.setCatalogName(rowArray[4].toString());
            av.setContent(rowArray[5].toString());
            av.setReplyNumber(Integer.parseInt(rowArray[6].toString()));
            av.setTags(rowArray[7].toString());
            av.setTitle(rowArray[8].toString());
            av.setQid(rowArray[9].toString());
            av.setQname(rowArray[10].toString());
            av.setUid(Integer.parseInt(rowArray[11].toString()));
            av.setUsername(rowArray[12].toString());

            articleViewList.add(av);
        }
        return articleViewList;
    }


    @Override
    public List<ArticleView> queryAllArticlePager(Pager<ArticleView> pager) {
        //Pager<ArticleView> pager = new Pager<ArticleView>();
        List<ArticleView> bigList = queryAllArticles();
        System.out.println("记录总数："+bigList.size());
        pager.setBigList(bigList);

        System.out.println("小集合数量："+pager.getSmallList().size());
        return pager.getSmallList();
    }


    @Override
    public List<ArticleView> queryAllArticleByCatalogName(String catalogName) {
        Object[] objectArr = articleDao.queryAllArticleViewsByCatalogName(catalogName);
        List<ArticleView> articleViewList = new ArrayList<ArticleView>();
        for(Object row: objectArr){
            ArticleView av = new ArticleView();
            Object[] rowArray = (Object[])row;
            av.setAid(Integer.parseInt(rowArray[0].toString()));
            if(rowArray[1]!=null) {
                av.setCreateTime(rowArray[1].toString());
            }
            av.setBrowseNumber(Integer.parseInt(rowArray[2].toString()));
            av.setCid(rowArray[3].toString());
            av.setCatalogName(rowArray[4].toString());
            av.setContent(rowArray[5].toString());
            av.setReplyNumber(Integer.parseInt(rowArray[6].toString()));
            av.setTags(rowArray[7].toString());
            av.setTitle(rowArray[8].toString());
            av.setQid(rowArray[9].toString());
            av.setQname(rowArray[10].toString());
            av.setUid(Integer.parseInt(rowArray[11].toString()));
            av.setUsername(rowArray[12].toString());

            articleViewList.add(av);
        }
        return articleViewList;
    }

    @Override
    public List<ArticleView> queryAllArticleByQuestionType(String questionType) {
        Object[] objectArr = articleDao.queryAllArticleViewsByQuestionTypename(questionType);
        List<ArticleView> articleViewList = new ArrayList<ArticleView>();
        for(Object row: objectArr){
            ArticleView av = new ArticleView();
            Object[] rowArray = (Object[])row;
            av.setAid(Integer.parseInt(rowArray[0].toString()));
            if(rowArray[1]!=null) {
                av.setCreateTime(rowArray[1].toString());
            }
            av.setBrowseNumber(Integer.parseInt(rowArray[2].toString()));
            av.setCid(rowArray[3].toString());
            av.setCatalogName(rowArray[4].toString());
            av.setContent(rowArray[5].toString());
            av.setReplyNumber(Integer.parseInt(rowArray[6].toString()));
            av.setTags(rowArray[7].toString());
            av.setTitle(rowArray[8].toString());
            av.setQid(rowArray[9].toString());
            av.setQname(rowArray[10].toString());
            av.setUid(Integer.parseInt(rowArray[11].toString()));
            av.setUsername(rowArray[12].toString());

            articleViewList.add(av);
        }
        return articleViewList;
    }

    @Override
    public List<ArticleView> queryArticlePagerByCatalogName(String catalogName, Pager<ArticleView> pager) {
        List<ArticleView> bigList = queryAllArticleByCatalogName(catalogName);
        System.out.println("记录总数："+bigList.size());
        pager.setBigList(bigList);

        System.out.println("小集合数量："+pager.getSmallList().size());

        return pager.getSmallList();
    }

    @Override
    public List<ArticleView> queryArticlePagerByQuestionType(String questionType, Pager<ArticleView> pager) {
        List<ArticleView> bigList = queryAllArticleByQuestionType(questionType);
        System.out.println("记录总数："+bigList.size());
        pager.setBigList(bigList);

        System.out.println("小集合数量："+pager.getSmallList().size());

        return pager.getSmallList();
    }
}
